package com.lw.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author LW
 * @2020/9/23 18:06
 */
//通过反射获取泛型
public class Test10 {

    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Method method = Test10.class.getDeclaredMethod("test01", Map.class, List.class);
        Type[] genericParameterTypes = method.getGenericParameterTypes();//获取通用参数类型
        for (int i = 0; i < genericParameterTypes.length; i++) {
            System.out.println("#"+genericParameterTypes[i]);
            if (genericParameterTypes[i] instanceof ParameterizedType){//如果这个泛型的参数类型属于一个参数化类型

                Type[] actualTypeArguments = ((ParameterizedType) genericParameterTypes[i]).getActualTypeArguments();
                //actualTypeArguments   实际类型参数
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }

            }
        }


    }
}
